// hyb_yl/mysubpages/pages/phyDetail/phyDetail.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 0,
    item: {},
    tyType: false,
    tyTypeindex: false,
    tyTypeindex2: false,
    time1: null
  },
  tyxqBtn: function (e) {
    var name = e.currentTarget.dataset.name;
    //生成二维码
    this.setData({
      tyName: name
    })
  },
  plase(e) {
    console.log(e)
    var that = this
    var num = e.detail.value
    that.setData({
      num: num
    })
  },
  closeZhe: function () {
    //添加数据
    var that = this
    var title = that.data.title
    var id = that.data.tjid
    var type = that.data.type
    var orderid = that.data.id
    var num = that.data.num
    console.log(num)
    if (num == '') {
      wx.showToast({
        title: '数据不能为空',
        icon: 'none'
      })
      return false;
    }
    app.util.request({
      'url': 'entry/wxapp/tijian.addjson',
      data: {
        title: title,
        id: id,
        orderid: orderid,
        type: type,
        num: num
      },
      success: function (res) {
        console.log(res)
        that.getAlltijian()
        that.setData({
          tyType: false,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var useropenid = wx.getStorageSync('openid')
    var bgc = wx.getStorageSync('color')

    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })




    if (options.scene) {
      var scene = decodeURIComponent(options.scene)
      var tjid = scene.split("&")[0]
      var id = scene.split("=")[1].split("&")[0];
      var type = scene.split("&")[2].split("=")[1];
      //查询当前机构的管理员
      app.util.request({
        'url': 'entry/wxapp/index.hospitaladmin',
        data: {
          id: id
        },
        success: function (res) {
          var adminopenid = res.data
          that.setData({
            adminopenid: adminopenid
          })
          if (adminopenid !== useropenid || useropenid == '') {
            wx.showModal({
              title: '提示',
              content: '您暂无权限，或授权失效，请重新授权',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.reLaunch({
                    url: '/hyb_yl/tabBar/index/index',
                  })
                }
              }
            })
            return false;
          }
        }
      })
      //查询套餐名称
      app.util.request({
        'url': 'entry/wxapp/tijian.taocandetail',
        data: {
          tjid: tjid
        },
        success: function (res) {
          console.log(res)
          that.setData({
            title: res.data.title,
            tjid: tjid
          })
        }
      })
      that.setData({
        tjid: tjid,
        id: id,
        tyType: true,
        type: type
      })
    } else {
      var overtime = options.overtime
      var id = options.id
      var time3 = overtime.split('-')
      var time4 = time3.join('/')
      var date1 = new Date(time4)
      var ifpay = that.data.ifpay
      that.data.time1 = setInterval(function () {
        var date = new Date()
        var sheyuTime = Date.parse(date1) - Date.parse(date)
        let hours = parseInt((sheyuTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        let minutes = parseInt((sheyuTime % (1000 * 60 * 60)) / (1000 * 60));
        let seconds = (sheyuTime % (1000 * 60)) / 1000;
        that.setData({
          dumiao: hours + ":" + minutes + ":" + seconds
        })
        if (hours <= 0 && minutes <= 0 && seconds <= 0) {
          that.setData({
            dumiao: '00:00:00'
          })
          clearTimeout(that.data.time1)
        }
      }, 1000)
    }

    app.util.request({
      'url': 'entry/wxapp/tijian.oderinfo',
      data: {
        id: id
      },
      success: function (res) {
        that.setData({
          detail: res.data,
          content: res.data.content,
          addproject: res.data.addproject,
        })
      }
    })
    that.setData({
      id: id,
      overtime: overtime,
      bgc: bgc
    })


  },
  big_img: function (e) {
    var img_src = e.currentTarget.dataset.src
    wx.previewImage({
      urls: [img_src],
    })
  },
  paybtn(e) {
    var that = this
    var money = e.currentTarget.dataset.money
    var orders = e.currentTarget.dataset.orders
    app.util.request({
      'url': 'entry/wxapp/tijian.paytijianorder',
      header: {
        "Content-Type": "application/xml"
      },
      method: "GET",
      data: {
        openid: wx.getStorageSync('openid'),
        z_tw_money: money,
        orders: orders
      },
      success: function (res) {
        console.log(res);
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr, //字符串随机数
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          success: function (res) {
            //跳转订单页
            wx.showToast({
              title: '支付成功',
              icon: 'none',
              duration: 1500,
              success: function (res) {
                wx.redirectTo({
                  url: '/hyb_yl/mysubpages/pages/physicalOrder/physicalOrder',
                })
              }
            })
          },
        })
      }
    })
  },
  openbtn(e) {
    //生成二维码 type=1 普通套餐 type=2 通用加包项
    var that = this
    var type = e.currentTarget.dataset.type
    var title = e.currentTarget.dataset.title
    var index = e.currentTarget.dataset.dex
    var id = e.currentTarget.dataset.id
    var orderid = that.data.id
    var ifcz = e.currentTarget.dataset.ifcz
    var content = this.data.content
    var addproject = this.data.addproject
    wx.showLoading({
      title: '体检码生成中，请稍后',
    })
    app.util.request({
      'url': 'entry/wxapp/tijian.erweima',
      data: {
        title: title,
        id: id,
        orderid: orderid,
        type: type,
        ifcz: ifcz
      },
      success: function (res) {

        console.log(res)
        that.getAlltijian(type, index)
        wx.hideLoading()
        that.setData({
          typeIndex: index,
          type: type,
          tyTypeindex: true
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  getAlltijian(type, index) {
    var that = this
    var id = that.data.id
    //查询体检订单
    app.util.request({
      'url': 'entry/wxapp/tijian.oderinfo',
      data: {
        id: id
      },
      success: function (res) {
        console.log(res)

        that.setData({
          detail: res.data,
          content: res.data.content,
          addproject: res.data.addproject,
        })
        var content = that.data.content,
          addproject = that.data.addproject;
        if (type == 1) {
          content[index].open = !content[index].open
        } else if (type == 2) {
          addproject[index].open = !addproject[index].open
        }
        that.setData({
          content: content,
          addproject: addproject,
        })
      }
    })
  },
  show() {
    wx.showToast({
      title: '支付后可查',
      icon: 'none'
    })
  },
  create() {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定已完成所有检查项',
      success(res) {
        if (res.confirm) {
          var id = that.data.id
          app.util.request({
            'url': 'entry/wxapp/tijian.updatebaogao',
            data: {
              id: id
            },
            success: function (res) {
              console.log(res)
              wx.showToast({
                title: '生成报告成功',
                icon: 'none',
                duration: 2000,
                success: function () {
                  wx.reLaunch({
                    url: '/hyb_yl/mysubpages/pages/physicalOrder/physicalOrder',
                  })
                }
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }

})